"use strict";
/* Controllers */
app.controller("fengkongDashboardController", ['$scope', '$location', '$rootScope', '$routeParams', 'fengkongDashboardService', '$q', function ($scope, $location, $rootScope, $routeParams, fengkongDashboardService, $q) {
    var vm = $scope.vm = {};

    vm.p = {};
    vm.r = {};
    vm.v = {};

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if ($scope.vm.p.dt == '' || typeof ($scope.vm.p.dt) == 'undefined') {
        $scope.vm.p.dt = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }


    var errorFn = function (result) {
        alert(result);
    };
    $scope.loadDashboard = function () {
        var vm = $scope.vm;
        // 初始化时间
        vm.p.startDt = new Date(new Date(vm.p.dt).getTime() - 86400000*6).format('yyyy.MM.dd');
        vm.p.endDt = new Date(vm.p.dt).format('yyyy.MM.dd');

        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                alert("请求失败：" + response.message);
                return;
            }
            vm.r = data
        };
        return fengkongDashboardService.loadDashboard(params, procFn, errorFn);
    };


    $scope.readFengkongV2 = function () {
        var vm = $scope.vm;
        // 初始化时间
        vm.p.startDt = new Date(new Date(vm.p.dt).getTime() - 86400000*6).format('yyyy.MM.dd');
        vm.p.endDt = new Date(vm.p.dt).format('yyyy.MM.dd');

        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                alert("请求失败：" + response.message);
                return;
            }
            vm.v = data


            vm.v['all^arpu1'] = {};
            var uv = Number(vm.v['ganji^uv'].content) + Number(vm.v['58^uv'].content);
            var uv7= Number(vm.v['ganji^uv'].contentInc7) + Number(vm.v['58^uv'].contentInc7);
            var uv365= Number(vm.v['ganji^uv'].contentInc365) + Number(vm.v['58^uv'].contentInc365);
            vm.v['all^arpu1'].content = (vm.v['all^gaap'].content/uv).toFixed(4);
            vm.v['all^arpu1'].contentInc7 = (vm.v['all^gaap'].contentInc7/uv7).toFixed(4);
            vm.v['all^arpu1'].contentInc365 = (vm.v['all^gaap'].contentInc365/uv365).toFixed(4);

            vm.v['all^arpu'] = {};
            vm.v['all^arpu'].content = (vm.v['all^gaap'].content/vm.v['all^has_cost_buv'].content).toFixed(4);
            vm.v['all^arpu'].contentInc7 = (vm.v['all^gaap'].contentInc7/vm.v['all^has_cost_buv'].contentInc7).toFixed(4);
            vm.v['all^arpu'].contentInc365 = (vm.v['all^gaap'].contentInc365/vm.v['all^has_cost_buv'].contentInc365).toFixed(4);

            // 计算所有的inc7和inc365
            for(var i in vm.v) {
                var it = vm.v[i];
                it.inc7 = ((it.content-it.contentInc7)/it.contentInc7).toFixed(4);
                it.inc365 = ((it.content-it.contentInc365)/it.contentInc365).toFixed(4);
            }
        };
        return fengkongDashboardService.readFengkongV2(params, procFn, errorFn);
    };

    $scope.queryDashboard = function() {
        $scope.loadDashboard();
        $scope.readFengkongV2();
    }

    $scope.queryDashboard()

}]);

/* Services */
app.service('fengkongDashboardService', ['ajaxService', function (ajaxService) {

    this.loadDashboard = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/fengkong/loadDashboard.do", successFunction, errorFunction);
    };

    this.readFengkongV2 = function (params, successFunction, errorFunction) {
        return ajaxService.AjaxPost(params, "/star/api/basic-service/v1/fengkong/readFengkongV2.do", successFunction, errorFunction);
    };

}]);



